<template>
  <view class="user-tasks-container">
    <view class="header">
      <text class="title">{{ nickName ? nickName : '用户' }}的任务</text>
    </view>
    <view v-if="loading" class="loading">加载中...</view>
    <view v-else-if="tasks.length === 0" class="empty">暂无任务</view>
    <view v-else>
      <view v-for="item in tasks" :key="item.taskId" class="task-item" @click="goToTaskDetail(item.taskId)">
        <image v-if="item.imageUrls" :src="getFirstImage(item.imageUrls)" class="cover" mode="aspectFill" />
        <view class="info">
          <text class="task-title">{{ item.title || item.jobName }}</text>
          <text class="task-time">{{ item.createTime }}</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import config from '@/config/config.js'
export default {
  data() {
    return {
      userId: '',
      nickName: '',
      tasks: [],
      loading: true
    }
  },
  onLoad(options) {
    this.userId = options.userId
    this.nickName = decodeURIComponent(options.nickName || '')
    if (!this.userId || this.userId === 'null') {
      uni.showToast({ title: '用户ID无效', icon: 'none' })
      this.loading = false
      return
    }
    this.loadTasks()
  },
  methods: {
    async loadTasks() {
      this.loading = true
      try {
        const token = uni.getStorageSync('token')
        const res = await new Promise((resolve, reject) => {
          uni.request({
            url: config.baseUrl + (config.apiPrefix || '') + `/system/task/list?userId=${this.userId}`,
            method: 'GET',
            header: {
              'Authorization': token
            },
            success: resolve,
            fail: reject
          })
        })
        if (res.data && res.data.code === 200) {
          this.tasks = res.data.rows || []
        } else {
          uni.showToast({ title: (res.data && res.data.msg) || '获取任务失败', icon: 'none' })
        }
      } catch (e) {
        uni.showToast({ title: '网络错误', icon: 'none' })
      } finally {
        this.loading = false
      }
    },
    goToTaskDetail(taskId) {
      uni.navigateTo({ url: `/pages/task-detail/task-detail?taskId=${taskId}` })
    },
    getFirstImage(urls) {
      if (!urls) return ''
      const arr = urls.split(',')
      if (arr[0]) {
        if (/^https?:\/\//.test(arr[0])) {
          return arr[0]
        } else {
          return config.baseUrl + (config.apiPrefix || '') + arr[0]
        }
      }
      return ''
    }
  }
}
</script>

<style>
.user-tasks-container {
  padding: 30rpx;
}
.header {
  text-align: center;
  margin-bottom: 30rpx;
}
.title {
  font-size: 36rpx;
  font-weight: bold;
  color: #333;
}
.task-item {
  display: flex;
  align-items: center;
  padding: 20rpx 0;
  border-bottom: 1rpx solid #f5f5f5;
  cursor: pointer;
}
.cover {
  width: 100rpx;
  height: 100rpx;
  border-radius: 12rpx;
  margin-right: 20rpx;
  object-fit: cover;
}
.info {
  flex: 1;
  display: flex;
  flex-direction: column;
}
.task-title {
  font-size: 30rpx;
  color: #333;
  font-weight: bold;
  margin-bottom: 10rpx;
}
.task-time {
  font-size: 24rpx;
  color: #999;
}
.loading, .empty {
  text-align: center;
  color: #999;
  font-size: 28rpx;
  margin-top: 100rpx;
}
</style> 